<?php
include_once '../../config.php';
include_once '../../lib/database.php';
include_once '../../functions.php';

if(isset($_GET['action']) && $_GET['action'] == 'search'){
	
	$select = 'SELECT id, joketext';
	$from = ' FROM joke';
	$where = ' WHERE TRUE';
	
	$authorid = $_GET['author'];
	if($authorid != ''){
		$where .= " AND authorid='$authorid'";
	}
	
	$categoryid = $_GET['category'];
	if($category != ''){
		$from .= " INNER JOIN jokecategory on id = jokeid";
		$where .= " AND categoryid='$categoryid'";
	}
	
	$text = $_GET['text'];
	if ($text != ''){
		$where .= " AND joketext LIKE '%$text%'";
	}
	
	$sql = $select . $from . $where;
	$jokes = $db->query($sql)->fetch_assoc();
	
	include 'jokes.php';
	exit();
}


//produces two arrays to be used to fill the dropdown on the searchform
$sql = "SELECT id, name FROM author LIMIT 0,10";
$authors = $db->query($sql)->fetch_assoc();


$sql = "SELECT id, name FROM category LIMIT 0,10";
$categories = $db->query($sql)->fetch_assoc();

include 'search_form.php';